package com.manlitech.cloud.manlifuncion.funcion;

import com.manlitech.cloud.manlifuncion.pojo.Foo;
import com.manlitech.cloudboot.common.utils.Result;
import com.manlitech.cloudboot.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author shensg
 * @date 2021/8/9 15:49
 */

@Component
@Slf4j
@RestController
public class ManliFunction {
    @Bean
    public Function<String, String> reverseString() {
        return value -> new StringBuilder(value).reverse().toString();
    }

    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }

    @Bean
    public Supplier<Flux<Foo>> words() {
        return () -> Flux.fromArray(new Foo[]{new Foo("foo"), new Foo("bar")}).log();
    }

    @Bean
    public Function<Foo, List> word() {
        return ss -> {
            log.info("调用word成功，入参: "+ss.toString());
            return Arrays.asList(ss.getValue().split(","));
        };
    }

    @Bean
    public Function<Flux<String>, Flux<String>> lowerCase() {
        return flux -> flux.map(value -> value.toLowerCase());
    }

    @Bean
    public Function<HttpServletRequestWrapper, Result> request() {
        return request -> {
            log.info("调用word成功，入参: "+request.getRequest().getAttribute("").toString());
            return ResultUtil.success("success");
        };
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}
